import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class FrontDesk extends User {

    private List<Screening> screenings;
    private List<Movie> movies;
    private List<Ticket> tickets;
    public FrontDesk(String userId, String username, String password, String phone,
                     List<Screening> screenings, List<Movie> movies, List<Ticket> tickets) {
        super(userId, username, password, UserType.FRONT_DESK, phone);
        this.screenings = screenings;
        this.movies = movies;
        this.tickets = tickets;
    }

    @Override
    public void menu() {
        while (true) {
            System.out.println("\n===== 前台菜单 =====");
            System.out.println("1. 列出所有影片");
            System.out.println("2. 列出所有场次");
            System.out.println("3. 查看场次座位");
            System.out.println("4. 售票");
            System.out.println("5. 退票");
            System.out.println("6. 修改密码");
            System.out.println("7. 退出登录");
            System.out.print("请选择: ");

            try {
                int choice = Integer.parseInt(scanner.nextLine());
                switch (choice) {
                    case 1:
                        listAllMovies();
                        break;
                    case 2:
                        listScreenings();
                        break;
                    case 3:
                        showScreeningSeats();
                        break;
                    case 4:
                        sellTicket();
                        break;
                    case 5:
                        refundTicket();
                        break;
                    case 6:
                        changeOwnPassword();
                        break;
                    case 7:
                        return;
                    default:
                        System.out.println("无效选择");
                }
            } catch (NumberFormatException e) {
                System.out.println("请输入有效数字");
            }
        }
    }
    // 列出所有影片
    private void listAllMovies() {
        System.out.println("\n===== 影片列表 =====");
        System.out.printf("%-10s %-20s %-15s %-30s %-8s %-5s%n",
                "电影ID", "片名", "导演", "主演", "时长(分)", "评分");

        for (Movie movie : movies) {
            System.out.printf("%-10s %-20s %-15s %-30s %-8d %-5.1f%n",
                    movie.getMovieId(),
                    movie.getTitle(),
                    movie.getDirector(),
                    movie.getActors().length() > 25 ? movie.getActors().substring(0, 25) + "..." : movie.getActors(),
                    movie.getDuration(),
                    movie.getRating());
        }
    }

    // 列出场次
    private void listScreenings() {
        System.out.println("\n===== 场次列表 =====");
        System.out.printf("%-10s %-20s %-15s %-20s %-10s %-10s%n",
                "场次ID", "电影", "放映厅", "时间", "票价", "剩余座位");

        for (Screening screening : screenings) {
            System.out.printf("%-10s %-20s %-15s %-20s %-10.2f %-10d%n",
                    screening.getScreeningId(),
                    screening.getMovie().getTitle(),
                    screening.getHall().getName(),
                    screening.getStartTime().toString(),
                    screening.getPrice(),
                    screening.getAvailableSeatsCount());
        }
    }

    // 查看场次座位
    private void showScreeningSeats() {
        System.out.println("\n===== 查看场次座位 =====");
        listScreenings();
        System.out.print("请输入场次ID: ");
        String screeningId = scanner.nextLine();

        Screening screening = null;
        for (Screening s : screenings) {
            if (s.getScreeningId().equals(screeningId)) {
                screening = s;
                break;
            }
        }

        if (screening == null) {
            System.out.println("场次不存在");
            return;
        }

        System.out.println("\n场次信息:");
        System.out.println("电影: " + screening.getMovie().getTitle());
        System.out.println("放映厅: " + screening.getHall().getName());
        System.out.println("时间: " + screening.getStartTime());
        System.out.println("票价: " + screening.getPrice());
        System.out.println("剩余座位: " + screening.getAvailableSeatsCount() + "/" + screening.getHall().getCapacity());

        System.out.println("\n座位图 (X: 已售, O: 可用):");
        screening.printSeatMap();
    }

    // 售票
    private void sellTicket() {
        System.out.println("\n===== 售票功能 =====");
        listScreenings();
        System.out.print("请输入场次ID: ");
        String screeningId = scanner.nextLine();

        Screening screening = null;
        for (Screening s : screenings) {
            if (s.getScreeningId().equals(screeningId)) {
                screening = s;
                break;
            }
        }

        if (screening == null) {
            System.out.println("场次不存在");
            return;
        }

        if (screening.getStartTime().isBefore(LocalDateTime.now())) {
            System.out.println("该场次已开始，不能售票");
            return;
        }

        System.out.println("\n座位图:");
        screening.printSeatMap();

        List<String> selectedSeats = new ArrayList<>();
        double totalPrice = 0;
        int adultCount = 0;
        int childCount = 0;

        System.out.print("购票数量: ");
        int ticketCount = Integer.parseInt(scanner.nextLine());

        for (int i = 0; i < ticketCount; i++) {
            System.out.printf("选择第%d张票的座位 (格式: 排-列): ", i + 1);
            String seat = scanner.nextLine();
            String[] parts = seat.split("-");

            if (parts.length != 2) {
                System.out.println("格式错误，请重新输入");
                i--;
                continue;
            }

            try {
                int row = Integer.parseInt(parts[0]) - 1;
                int col = Integer.parseInt(parts[1]) - 1;

                if (screening.occupySeat(row, col)) {
                    selectedSeats.add((row + 1) + "-" + (col + 1));

                    System.out.print("票类型 (1.成人票 2.儿童票): ");
                    int type = Integer.parseInt(scanner.nextLine());
                    double ticketPrice = screening.getPrice();

                    if (type == 2) {
                        ticketPrice *= 0.7;
                        childCount++;
                    } else {
                        adultCount++;
                    }

                    if(screening.getStartTime().getHour() < 12) {
                        ticketPrice *= 0.9;
                    }

                    totalPrice += ticketPrice;
                } else {
                    System.out.println("座位不可用或无效");
                    i--;
                }
            } catch (NumberFormatException e) {
                System.out.println("格式错误，请重新输入");
                i--;
            }
        }

        Ticket ticket = new Ticket(screening, selectedSeats, totalPrice);
        tickets.add(ticket);

        System.out.println("\n===== 票信息 =====");
        System.out.println("票号: " + ticket.getTicketId());
        System.out.println("电影: " + screening.getMovie().getTitle());
        System.out.println("场次: " + screening.getStartTime());
        System.out.println("放映厅: " + screening.getHall().getName());
        System.out.println("座位: " + String.join(", ", selectedSeats));
        System.out.println("成人票: " + adultCount + "张, 儿童票: " + childCount + "张");
        System.out.println("总价: " + totalPrice);
        System.out.println("购票时间: " + ticket.getPurchaseTime());
    }
    //退票
    private void refundTicket() {
        System.out.println("\n===== 退票功能 =====");
        System.out.print("请输入票号: ");
        String ticketId = scanner.nextLine();

        Ticket ticketToRefund = null;
        for (Ticket ticket : tickets) {
            if (ticket.getTicketId().equals(ticketId)) {
                ticketToRefund = ticket;
                break;
            }
        }

        if (ticketToRefund == null) {
            System.out.println("票不存在");
            return;
        }

        if (ticketToRefund.isRefunded()) {
            System.out.println("该票已退");
            return;
        }

        LocalDateTime screeningTime = ticketToRefund.getScreening().getStartTime();
        LocalDateTime now = LocalDateTime.now();

        if (now.isAfter(screeningTime.minusHours(1))) {
            System.out.println("电影开场前1小时内不能退票");
            return;
        }

        double refundAmount = ticketToRefund.getTotalPrice();
        if (now.isAfter(screeningTime.minusDays(1))) {
            refundAmount *= 0.9; // 提前1小时内退票，扣10%
            System.out.println("提前1小时内退票，扣除10%手续费");
        }

        if (ticketToRefund.refund()) {
            for (String seat : ticketToRefund.getSeats()) {
                String[] parts = seat.split("-");
                int row = Integer.parseInt(parts[0]) - 1;
                int col = Integer.parseInt(parts[1]) - 1;
                ticketToRefund.getScreening().releaseSeat(row, col);
            }

            System.out.println("退票成功");
            System.out.println("退款金额: " + refundAmount);
        }
    }

}